#include "FreeRTOS.h"
#include "task.h"
#include "main_task.h"
#include "bsp.h"
#include "bs32f1xx_ll_utils.h"
#include "bs32f1xx_ll_uart.h"
#include "stdio.h"
#include "input_event_handler.h"
#include "uart_printf.h"
#include "shell.h"

#define MAIN_TASK_SIZE 128
#define MAIN_TASK_NAME "main_task"
#define MAIN_TASK_PRI 2
TaskHandle_t main_task_handle;

#define LED_TASK_SIZE 128
#define LED_TASK_NAME "led_task"
#define LED_TASK_PRI 2
TaskHandle_t led_task_handle;


void main_task(void*param);
void app_led_task(void*param);

void app_task_init(void)
{
	BaseType_t rval;
	rval = xTaskCreate(	main_task,
						MAIN_TASK_NAME,
						MAIN_TASK_SIZE,
						NULL,
						MAIN_TASK_PRI,
						&main_task_handle);

	rval = xTaskCreate(	app_led_task,
						LED_TASK_NAME,
						LED_TASK_SIZE,
						NULL,
						LED_TASK_PRI,
						&led_task_handle);
	

	rval = input_event_init();
	shell_task_init();
	
	if(rval == pdPASS){
		vTaskStartScheduler();
	}
}


void main_task(void*param)
{
	uint16_t event = 0x00;

	while(1){
		xQueueReceive(input_event_queue,&event,portMAX_DELAY);
		switch(event){
			case KEY_EVENT_UP:
			{
				static uint8_t led_flg = 0;				
				PRINTF("KEY_EVENT_UP\r\n");
				led_flg = 1-led_flg;
				led_control(LED_1,led_flg);
			}
				break;
			case KEY_EVENT_0:
			{	
				static uint8_t beep_flg = 0;
				beep_flg = 1-beep_flg;
				beep_control(beep_flg);
				PRINTF("KEY_EVENT_0\r\n");
			}
				break;
			case KEY_EVENT_1:
				PRINTF("KEY_EVENT_1\r\n");
				break;
			default:
				PRINTF("msg[ 0x%x ]this is main task..\r\n",event);
				break;				
		}				
	}
}

void app_led_task(void*param)
{
	const TickType_t xDelay = 500 / portTICK_PERIOD_MS;
	while(1){
		led_control(LED_B,0);
		vTaskDelay(xDelay);
		led_control(LED_B,1);
		vTaskDelay(xDelay);
	}

}
